package com.wn.icloud.provider.uac.controller.lx;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wn.icloud.common.msg.ObjectRestCode;
import com.wn.icloud.common.msg.ObjectRestResponse;
import com.wn.icloud.common.utils.ApacheHttpUtils;
import com.wn.icloud.common.utils.LingXiAESUtil;
import com.wn.icloud.provider.uac.constant.LingXiApiUrlConstant;
import com.wn.icloud.provider.uac.service.dto.EnterpriseInfoDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/lx")
public class LingXiController {
    private static Logger logger = LoggerFactory.getLogger(LingXiController.class);


    /**
     * 校验企业信息
     */
    @PostMapping("checkCompanyInfo")
    public ObjectRestResponse checkCompanyInfo(@RequestBody EnterpriseInfoDTO enterpriseInfoDTO){
        Map<String,String> reqMap = new HashMap<>();
        reqMap.put("companyName",enterpriseInfoDTO.getName() );//单位名称
        reqMap.put("creditCode",enterpriseInfoDTO.getSocialCreditCode() );//社会信用代码
        reqMap.put("legalPersonName",enterpriseInfoDTO.getLegalPersonName() );//法人姓名
        reqMap.put("idCard",enterpriseInfoDTO.getLegalPersonIdCard());//法人身份证号
        String resultData = LingXiController.lingXiReq(JSON.toJSONString(reqMap), LingXiApiUrlConstant.checkCompanyInfo);
        JSONObject jsonObject = JSONObject.parseObject(resultData);
        int code =(int) jsonObject.get("code");
        if (code == 0){
            String data = jsonObject.getString("data");
            JSONObject jsonDataObject = JSONObject.parseObject(data);
            return ObjectRestResponse.success(jsonDataObject);
        }else {
            return ObjectRestResponse.failure(ObjectRestCode.UNKNOWN_ERROR);
        }
    }


    /**
     * 获取企业画像
     */
    @PostMapping("companyPortraits")
    public ObjectRestResponse sendCompanyRegisterInfo(@RequestBody EnterpriseInfoDTO enterpriseInfoDTO){
        Map<String,String> reqMap2 = new HashMap<>();
        reqMap2.put("companyName",enterpriseInfoDTO.getName());//企业名称
        reqMap2.put("creditCode",enterpriseInfoDTO.getSocialCreditCode() );//信用代码
        String resultData = LingXiController.lingXiReq(JSON.toJSONString(reqMap2), LingXiApiUrlConstant.companyPortraits);
        JSONObject jsonObject = JSONObject.parseObject(resultData);
        int code =(int) jsonObject.get("code");
        if (code == 0){
            String data = jsonObject.getString("data");
            JSONObject jsonDataObject = JSONObject.parseObject(data);
            return ObjectRestResponse.success(jsonDataObject);
        }else {
            return ObjectRestResponse.failure(ObjectRestCode.UNKNOWN_ERROR);
        }
    }



    /**
     *
     * @param data 请求数据
     * @param apiUrl api接口地址
     * @return
     */
    public static String lingXiReq(String data,String apiUrl){
        String guid = LingXiAESUtil.getGUID();//偏移量
        final String encoded = LingXiAESUtil.encrypt(data, guid);
        Map<String,String> jsonReq = new HashMap<>();
        jsonReq.put("iv", guid);
        jsonReq.put("data", encoded);
        String response = ApacheHttpUtils.getInstance().sendHttpPost(LingXiApiUrlConstant.reqUrl + apiUrl,jsonReq).getData();
        JSONObject jsonResult = JSONObject.parseObject(response);
        if (jsonResult.get("iv") == null){
            return "0";
        }
        String vi = jsonResult.getString("iv");
        return LingXiAESUtil.decrypt(jsonResult.getString("data"), vi);
    }

    public static void main(String[] args) {

    }
}

